﻿using HalconDotNet;
using Halcon二次开发.Model;
using Halcon二次开发.Model.自定义控件;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Halcon二次开发.Controller.创建计量模型
{
    /// <summary>
    /// 创建计量模型
    /// </summary>
    public class MeasurementController
    {
        public HTuple hv_MetrologyHandle, hv_Row, hv_Column, hv_Radius, hv_circle_,hv_Index, hv_Row1, hv_Column1, hv_Parameter
            , hv_Parameter_row, hv_Parameter_column;
        public HObject ho_Contour, ho_Contours, ho_Contour1;
        /// <summary>
        /// 当前控件对象
        /// </summary>
        private HSmartWindowControl hw;
        /// <summary>
        /// 当前图像对象
        /// </summary>
        HObject HImage;

        HwindowFlowNodeUrseControl HwindowFlowNode;
        public MeasurementController(HwindowFlowNodeUrseControl HwindowFlowNode)
        {
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenEmptyObj(out ho_Contours);
            HOperatorSet.GenEmptyObj(out ho_Contour1);
            //获取当前工位
            hw = HwindowFlowNode.hSmartWindowControl1;
            this.HwindowFlowNode = HwindowFlowNode;

        }
        public void Action(string filePath,string nodes)
        {
            HTuple hv_WindowHandle = hw.HalconWindow;
            //需要一直获取
            HImage = HwindowFlowNode.hImage;
            Task.Run(() =>
            {
                try
                {
                    HOperatorSet.ClearWindow(hv_WindowHandle);
                    HOperatorSet.DispImage(HwindowFlowNode.hImage,hv_WindowHandle);
                    HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);
                    HOperatorSet.DrawCircle(hv_WindowHandle, out hv_Row, out hv_Column, out hv_Radius);
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_circle_ = new HTuple();
                        hv_circle_ = hv_circle_.TupleConcat(hv_Row, hv_Column, hv_Radius);
                    }
                    //**************添加几何形状************************
                    HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "circle", hv_circle_,
                    100, 5, 1, 30, (new HTuple("measure_distance")).TupleConcat("num_measures"),
                    (new HTuple(12)).TupleConcat(10), out hv_Index);
                    //获取添加的几何形状
                    HOperatorSet.GetMetrologyObjectModelContour(out ho_Contour, hv_MetrologyHandle,
                        "all", 1.5);
                    //获取几何形状的测量区域
                    HOperatorSet.GetMetrologyObjectMeasures(out ho_Contours, hv_MetrologyHandle,
                        "all", "all", out hv_Row1, out hv_Column1);
                    //*************设置 计量模型 参数********************
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", (new HTuple("num_measures")).TupleConcat(
                        "measure_length1"), (new HTuple(5)).TupleConcat(100));
                    //获取添加的几何形状
                    HOperatorSet.GetMetrologyObjectModelContour(out ho_Contour, hv_MetrologyHandle,
                    "all", 1.5);
                    //获取几何形状的测量区域
                    HOperatorSet.GetMetrologyObjectMeasures(out ho_Contours, hv_MetrologyHandle,
                    "all", "all", out hv_Row1, out hv_Column1);
                    //*************开始寻找(测量并拟合)***********************
                    //测量并拟合
                    HOperatorSet.ApplyMetrologyModel(HImage, hv_MetrologyHandle);
                    HOperatorSet.GetMetrologyObjectResultContour(out ho_Contour1, hv_MetrologyHandle,
                     0, "all", 1.5);
                    //*************获取寻找完成后的数据******************
                    //获取圆数据
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type",
                    "all_param", out hv_Parameter);
                    //获取测量矩形拟合出来的点
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, 0, "all", "used_edges",
                    "row", out hv_Parameter_row);
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, 0, "all", "used_edges",
                        "column", out hv_Parameter_column);
                    //*************显示/验证数据*****************************
                    //显示圆心和半径
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        HOperatorSet.DispCross(hv_WindowHandle, hv_Parameter.TupleSelect(0), hv_Parameter.TupleSelect(
                            1), 25, 0);
                    }

                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        HOperatorSet.DispLine(hv_WindowHandle, hv_Parameter.TupleSelect(0), hv_Parameter.TupleSelect(
                            1), (hv_Parameter.TupleSelect(0)) + (hv_Parameter.TupleSelect(2)), hv_Parameter.TupleSelect(
                            1));
                    }

                    //显示测量矩形拟合出来的点
                    for (int hv_Index1 = 0; (int)hv_Index1 <= (int)((new HTuple(hv_Parameter_row.TupleLength()
                        )) - 1); hv_Index1 = (int)hv_Index1 + 1)
                    {
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            HOperatorSet.DispCross(hv_WindowHandle, hv_Parameter_row.TupleSelect(hv_Index1),
                                hv_Parameter_column.TupleSelect(hv_Index1), 15, 0);
                        }

                    }
                    //*************清除储存*******************************
                    //删除计量对象
                    //clear_metrology_object (MetrologyHandle, 1)

                    HOperatorSet.WriteMetrologyModel(hv_MetrologyHandle, filePath + "计量模型文件" + "\\" + nodes + "计量模型.mtr");
                }
                catch (Exception e)
                {
                    
                }
            });
        }
    }
}
